//
//  YZEDBHelper.h
//  YZEDBHelper
//
//  Created by zjs on 17/3/16.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import <Foundation/Foundation.h>

#define DBName_Default @"yltx.db"
NS_ASSUME_NONNULL_BEGIN
@interface YZEDBHelper : NSObject


+(instancetype)sharedInstace;


/**
 设置数据库的路径及数据库名

 @param filePath path
 @param dbName dbname
 */
-(void)setDBFilePath:(NSString*)filePath dbName:(NSString*)dbName;

//得到数据库路径
-(NSString*)getDBDefaultFilePath;

#pragma mark 插入表数据
/**
 insert－同步
 
 @param model 需要插入的对象
 @return 成功／fail
 */
- (BOOL)insertToDB:(NSObject *)model;

/**
 insert－异步
 
 @param model 需要插入的对象
 */
- (void)insertToDB:(NSObject *)model callback:(void (^)(BOOL result))block;

/**
 insert－同步
 
 @param models 需要插入的对象数组
 */
-(void)insertToDBWithModels:(NSMutableArray*)models;


#pragma mark 更新数据库
/**
 更新数据库
 
 @param model model
 @param where 条件
 @return success/fail
 */
- (BOOL)updateToDB:(NSObject *)model where:(nullable id)where;


/**
 更新数据库
 
 @param model model
 @param where @{@"name":@"tim"}－－@“\"name\"=\"tim\"”
 @param block block
 */
- (void)updateToDB:(NSObject *)model where:(nullable id)where callback:(void (^)(BOOL result))block;

#pragma mark 删除表数据
/**
 *	@brief	delete table
 *
 *	@param 	model 	you want to delete entity
 when entity property "rowid" == 0  based on the primary key to delete
 *
 *	@return	the deleted was successful
 */
- (BOOL)deleteToDB:(NSObject *)model;
- (void)deleteToDB:(NSObject *)model callback:(void (^)(BOOL result))block;

/**
 *	@brief	delete table with "where" constraint
 *
 *	@param 	modelClass      entity class
 *	@param 	where           can use NSString or NSDictionary,  can not is nil
 *
 *	@return	the deleted was successful
 */
- (BOOL)deleteWithClass:(Class)modelClass where:(nullable id)where;
- (void)deleteWithClass:(Class)modelClass where:(nullable id)where callback:(void (^)(BOOL result))block;

/**
 *	@brief	Clear data based on the entity class
 *
 *	@param 	modelClass 	entity class
 */
- (void)clearTableData:(Class)modelClass;

#pragma mark 删除表
///drop all table
- (void)dropAllTable;

///drop table with entity class
- (BOOL)dropTableWithClass:(Class)modelClass;

- (BOOL)dropTableWithTableName:(NSString *)tableName;


#pragma mark 查询表数据

/**
 *	@brief	查询符合条件的行数
 *
 *	@param 	modelClass      entity class
 *	@param 	where           can use NSString or NSDictionary or nil
 *
 *	@return	rows number
 */
- (NSInteger)rowCount:(Class)modelClass where:(nullable id)where;

//异步
- (void)rowCount:(Class)modelClass where:(nullable id)where callback:(void (^)(NSInteger rowCount))callback;


/**
 @brief	查询符合条件的行数
 
 @param tableName 表名
 @param where 条件
 @return rows
 */
- (NSInteger)rowCountWithTableName:(NSString *)tableName where:(nullable id)where;


/**
 *	@brief	query table
 *
 *	@param 	modelClass      entity class
 *	@param 	where           can use NSString or NSDictionary or nil
 
 *	@param 	orderBy         The Sort: Ascending "name asc",Descending "name desc"
 For example: @"rowid desc"x  or @"rowid asc"
 
 *	@param 	offset          Skip how many rows
 *	@param 	count           Limit the number
 *
 *	@return	query finished result is an array(model instance collection)
 */
- (nullable NSMutableArray *)search:(Class)modelClass
                              where:(nullable id)where
                            orderBy:(nullable NSString *)orderBy
                             offset:(NSInteger)offset
                              count:(NSInteger)count;

/**
 *  query sql, query finished result is an array(model instance collection)
 *  you can use the "@t" replace Model TableName
 *  query sql use lowercase string
 *  查询的sql语句 请使用小写 ，否则会不能自动获取 rowid
 *  example:
 NSMutableArray *array = [[LKDBHelper getUsingLKDBHelper] searchWithSQL:@"select * from @t where blah blah.." toClass:[ModelClass class]];
 *
 */
- (nullable NSMutableArray *)searchWithSQL:(NSString *)sql toClass:(nullable Class)modelClass;

/**
 *  @brief 对sql语句不做任何变化
 */
- (nullable NSMutableArray *)searchWithRAWSQL:(NSString *)sql toClass:(nullable Class)modelClass;

/**
 *  query sql, query finished result is an array(model instance collection)
 *  you can use the "@t" replace Model TableName and replace all ? placeholders with the va_list
 *  example:
 NSMutableArray *array = [[LKDBHelper getUsingLKDBHelper] searc:[ModelClass class] withSQL:@"select rowid from name_table where name = ?", @"Swift"];
 *
 */
- (nullable NSMutableArray *)search:(Class)modelClass withSQL:(NSString *)sql, ...;

/** 同步
 columns may NSArray or NSString   if query column count == 1  return single column string array
 other return models entity array
 */
- (nullable NSMutableArray *)search:(Class)modelClass
                             column:(nullable id)columns
                              where:(nullable id)where
                            orderBy:(nullable NSString *)orderBy
                             offset:(NSInteger)offset
                              count:(NSInteger)count;

/**
 *  @brief  async search
 */
- (void)search:(Class)modelClass
         where:(nullable id)where
       orderBy:(nullable NSString *)orderBy
        offset:(NSInteger)offset
         count:(NSInteger)count
      callback:(void (^)(NSMutableArray * _Nullable array))block;

//返回第一个符合条件的Model
- (nullable id)searchSingle:(Class)modelClass where:(nullable id)where orderBy:(nullable NSString *)orderBy;

/**
 *	@brief   entity exists?
 *           for primary key column
 （if rowid > 0 would certainly exist so we do not rowid judgment）
 *	@param 	model 	entity
 *
 *	@return	YES: entity presence , NO: entity not exist
 */
- (BOOL)isExistsModel:(NSObject *)model;
- (BOOL)isExistsClass:(Class)modelClass where:(nullable id)where;
- (BOOL)isExistsWithTableName:(NSString *)tableName where:(nullable id)where;
@end

NS_ASSUME_NONNULL_END
